package Test;

import java.text.SimpleDateFormat;

import java.util.Date;

import junit.framework.Assert;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import ControlLayer.RoomBookingException;
import ControlLayer.RoomTypeCtr;
import ControlLayer.RBDateCtr;
import ModelLayer.KeyWord;
import ModelLayer.RBDate;
import ModelLayer.RoomType;

public class RBDateCtrTest {
	private RBDateCtr dateCtr;
	private RoomType rt;
	private RBDate date1;
	private RBDate date2;
	private Date thedate1;
	private Date thedate2;
	private RoomTypeCtr rtCtr;

	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
	}

	@AfterClass
	public static void tearDownAfterClass() throws Exception {
	}

	@Before
	public void setUp() throws Exception {
		thedate1 = new SimpleDateFormat("dd/MM/yyyy").parse("31/01/2013");
		thedate2 = new SimpleDateFormat("dd/MM/yyyy").parse("01/02/2013");
		dateCtr = new RBDateCtr();
		rt = new RoomType();
		rt.setName(KeyWord.DoubleRoom);
		rt.setPricePerNight(100);
		rt.setSize(2);
		date1 = new RBDate();
		date1.setNrOfAvailableRoom(1);
		date1.setRoomType(rt);
		date1.setDate(thedate1);
		date2 = new RBDate();
		date2.setNrOfAvailableRoom(1);
		date2.setRoomType(rt);
		date2.setDate(thedate2);
		rtCtr = new RoomTypeCtr();
	}

	@After
	public void tearDown() throws Exception {
	}

	@Test
	public void testIsRoomsAvailableForRT() {
		int rtId = rtCtr.insertRoomType("DoubleRoom", 100, 2);
		dateCtr.insertRBDate(rtId, thedate1, 1);
		dateCtr.insertRBDate(rtId, thedate2, 1);
		Assert.assertEquals(true,
				dateCtr.isRoomsAvailableForRT(rtId, 1, thedate1, thedate2));
		Assert.assertEquals(false,
				dateCtr.isRoomsAvailableForRT(rtId, 2, thedate1, thedate2));
		dateCtr.deleteRBDate(rtId, thedate1);
		dateCtr.deleteRBDate(rtId, thedate2);
		rtCtr.deleteRoomType(rtId);
	}

	@Test
	public void testInsertAndGetRBDate() {
		int rtId = rtCtr.insertRoomType("DoubleRoom", 100, 2);
		dateCtr.insertRBDate(rtId, thedate1, 1);
		RBDate rbDate = dateCtr.getRBDate(rtId, thedate1);
		Assert.assertEquals(rtId, rbDate.getRoomType().getId());
		Assert.assertEquals("31/01/2013",
				new SimpleDateFormat("dd/MM/yyyy").format(rbDate.getDate()));
		Assert.assertEquals(1, rbDate.getNrOfAvailableRoom());
		dateCtr.deleteRBDate(rtId, thedate1);
		rtCtr.deleteRoomType(rtId);

	}

	@Test
	public void testUpdateRBDate() {
		int rtId = rtCtr.insertRoomType("DoubleRoom", 100, 2);
		dateCtr.insertRBDate(rtId, thedate1, 1);
		dateCtr.updateRBDate(rtId, thedate1, 2);
		RBDate rbDate = dateCtr.getRBDate(rtId, thedate1);
		Assert.assertEquals(rtId, rbDate.getRoomType().getId());
		Assert.assertEquals("31/01/2013",
				new SimpleDateFormat("dd/MM/yyyy").format(rbDate.getDate()));
		Assert.assertEquals(2, rbDate.getNrOfAvailableRoom());
		dateCtr.deleteRBDate(rtId, thedate1);
		rtCtr.deleteRoomType(rtId);
	}

	@Test
	public void testDeleteRBDate() {
		int rtId = rtCtr.insertRoomType("DoubleRoom", 100, 2);
		dateCtr.insertRBDate(rtId, thedate1, 1);
		Assert.assertEquals(1, dateCtr.deleteRBDate(rtId, thedate1));
		dateCtr.deleteRBDate(rtId, thedate1);
		rtCtr.deleteRoomType(rtId);

	}

	@Test
	public void testIncrementNrOfAvaiRoom() {
		int rtId = rtCtr.insertRoomType("DoubleRoom", 100, 2);
		dateCtr.insertRBDate(rtId, thedate1, 1);
		dateCtr.insertRBDate(rtId, thedate2, 1);
		dateCtr.incrementNrOfAvaiRoom(rtId, thedate1, thedate2, 1);
		Assert.assertEquals(2, dateCtr.getRBDate(rtId, thedate1)
				.getNrOfAvailableRoom());
		Assert.assertEquals(2, dateCtr.getRBDate(rtId, thedate2)
				.getNrOfAvailableRoom());
		dateCtr.deleteRBDate(rtId, thedate1);
		dateCtr.deleteRBDate(rtId, thedate2);
		rtCtr.deleteRoomType(rtId);
	}

	@Test
	public void testDecrementNrOfAvaiRoom() {
		int rtId = rtCtr.insertRoomType("DoubleRoom", 100, 2);
		dateCtr.insertRBDate(rtId, thedate1, 1);
		dateCtr.insertRBDate(rtId, thedate2, 1);
		try {
			dateCtr.decrementNrOfAvaiRoom(rtId, thedate1, thedate2, 1);
		} catch (RoomBookingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Assert.assertEquals(0, dateCtr.getRBDate(rtId, thedate1)
				.getNrOfAvailableRoom());
		Assert.assertEquals(0, dateCtr.getRBDate(rtId, thedate2)
				.getNrOfAvailableRoom());
		dateCtr.deleteRBDate(rtId, thedate1);
		dateCtr.deleteRBDate(rtId, thedate2);
		rtCtr.deleteRoomType(rtId);
	}

}
